﻿using System;
using System.Diagnostics;
using System.Windows.Input;
using Antares.VIEWs;
using AntaresShell.BaseClasses;
using AntaresShell.Common;
using AntaresShell.Common.MessageTemplates;
using AntaresShell.Logger;
using AntaresShell.NavigatorProvider;
using Repository.MODELs;
using Repository.Repositories;
using Windows.UI.ViewManagement;

namespace Antares.VIEWMODELs
{
    public class SnapViewModel : ViewModelBase
    {
        private DayItemModel _day;

        public DayItemModel Day
        {
            get { return _day; }
            set { SetProperty(ref _day, value); }
        }

        private ICommand _projectCommand;
        public ICommand ProjectCommand
        {
            get { return _projectCommand ?? (_projectCommand = new RelayCommand(ProjectCmd)); }
        }

        private ICommand _gohomeCommand;
        public ICommand GoHomeCommand
        {
            get { return _gohomeCommand ?? (_gohomeCommand = new RelayCommand(GoHomeCmd)); }
        }
        private ICommand _basicmonthCommand;
        public ICommand BasicMonthCommand
        {
            get { return _basicmonthCommand ?? (_basicmonthCommand = new RelayCommand(BasicMonthCmd)); }
        }

        private DayItemModel _selectedItem;
        public DayItemModel SelectedItem
        {
            get { return _selectedItem ?? (_selectedItem = _day); }
            set { SetProperty(ref _selectedItem, value); }
        }

        public SnapViewModel()
        {
            Day = new DayItemModel()
            {
                Today = DateTime.Now.Date,
            };
            Messenger.Instance.Register<Refresh>(RefreshAll);
            BindDay();
        }


        private void GoHomeCmd(object obj)
        {
            ApplicationView.TryUnsnap();
            Navigator.Instance.NavigateTo(typeof(TimelineWeekPage));
        }

        private void ProjectCmd(object obj)
        {
            ApplicationView.TryUnsnap();
            Navigator.Instance.NavigateTo(typeof(ProjectManagerPage), "#1:");
        }

        private void BasicMonthCmd(object obj)
        {
            ApplicationView.TryUnsnap();
            Navigator.Instance.NavigateTo(typeof(BasicMonthPage));
        }

        private async void BindDay()
        {
            try
            {
                Day.TaskList = await TaskRepository.Instance.GetTaskListFor(DateTime.Today);
                Day.WeatherModel = await WeatherRepository.Instance.GetWeather(DateTime.Now) ??
                                             new DayWeatherModel
                                             {
                                                 AverageTempuratureC = null,
                                                 AverageTempuratureF = null,
                                                 WeatherType = WeatherType.Notavailable
                                             };

            }
            catch (Exception ex)
            {
                LogManager.Instance.LogException(ex.Message);
            }
        }

        private void RefreshAll(object obj)
        {
            Messenger.Instance.Notify(RebindCbo.RebindWeekGrid);
        }

        private async void BindTask(DayItemModel itemModel)
        {
            try
            {
                itemModel.TaskList = await TaskRepository.Instance.GetTaskListFor(itemModel.Today);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
        }
    }
}
